package com.apps.apicontroller;

import com.apps.common.model.po.ApiResult;
import com.apps.helper.UserConextHelper;
import com.apps.module.pay.model.vo.KupaPayDataVo;
import com.apps.order.enums.OrderStatus;
import com.apps.order.enums.PayType;
import com.apps.order.model.po.KupaVipGoodsOrder;
import com.apps.order.service.IKupaOrder;
import com.apps.order.service.IKupaVipGoodsOrderService;
import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.thirdparty.pay.wxpay.model.po.KupaWxpayReturn;
import com.apps.thirdparty.pay.wxpay.service.IKupaWxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(description = "用户支付和创建订单接口")
@RestController
@RequestMapping("/api/member/pay")
public class MemberPayApiController
{


    @Autowired
    private IKupaWxPayService wxPayService;

    @Autowired
    private IKupaVipGoodsOrderService vipGoodsOrderService;

    @Autowired
    List<IKupaOrder> kupaOrders;


    @ApiOperation(value = "创建支付订单",notes = "这个接口会生成微信支付二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "vip商品id", required = true,dataType = "int",paramType = "query")
    })
    @PostMapping("/CreatePayOrder")
    ApiResult<KupaPayDataVo> CreatePayOrder(@RequestParam(value = "id") Integer id)
    {
        ApiResult<KupaPayDataVo> result = new ApiResult<>();
        KupaVipGoodsOrder goodsOrder=vipGoodsOrderService.CreateOrder(UserConextHelper.getCurrentMember().getId(),id);
        if(null == goodsOrder)
            throw new NullException("创建订单失败");
        KupaWxpayReturn wxpayReturn=wxPayService.WxpayByNative(goodsOrder.getSn(),goodsOrder.getVip_goods_name(),goodsOrder.getPay_price());
        return result.success(new KupaPayDataVo(wxpayReturn.getCode_url(),goodsOrder));
    }


    @ApiOperation(value = "用下载币支付并开通VIP",notes = "这个接口会生成微信支付二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order_id", value = "待支付的开通VIP订单ID", required = true,dataType = "int",paramType = "query")
    })
    @PostMapping("/payOrderByAccount")
    ApiResult<String> payOrderByAccount(@RequestParam(value = "order_id") Integer order_id)
    {
        ApiResult<String> result = new ApiResult<>();
        KupaVipGoodsOrder goodsOrder=vipGoodsOrderService.getOneById(order_id);
        if(null == goodsOrder)
            throw new NullException("订单"+order_id+"不存在");
        for(IKupaOrder order:kupaOrders)
            order.PaySuccess(goodsOrder.getSn(),goodsOrder.getSn(),goodsOrder.getPay_price(), PayType.ACCOUNT);
        return result.success("支付成功");
    }

    @ApiOperation(value = "检查某个订单是否支付完成",notes = "用于前端检测是否关闭付款弹框")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order_sn", value = "订单编号", required = true,dataType = "string",paramType = "query")
    })
    @PostMapping("/CheckOrderStatus")
    ApiResult<String> CheckOrderStatus(@RequestParam(value = "order_sn") String order_sn)
    {
        ApiResult<String> result = new ApiResult<>();
        KupaVipGoodsOrder vipGoodsOrder=vipGoodsOrderService.getOneByFieldValue("sn",order_sn);
        if(null == vipGoodsOrder)
            throw new NullException("订单"+order_sn+"不存在");
        if(vipGoodsOrder.getOrder_status().equals(OrderStatus.CLOSE.name()))
            throw new DataException("订单"+order_sn+"已关闭");
        if(vipGoodsOrder.getOrder_status().equals(OrderStatus.PAYSUCESS.name()))
            return result.success("付款完成");
        return result.fail();
    }
}
